﻿/*-------------------------------------------------------------------------
* 命名空间: XrayDirection.DAL.LocalDataAccess/OrderAccess
* 类       名: OrderAccess
* 功      能:  TD
* 时      间:  2024/1/3 16:41:35
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using XrayDirection.Common;
using XrayDirection.DAL.DataEntity;
using XrayDirection.Extensions;

namespace XrayDirection.DAL.LocalDataAccess
{
    public class OrderAccess
    {

        /// <summary>
        /// 根据工单名称去获取工单数据,这里是在查询的时候去获取,然后只返回最后一条作业的工单信息.
        /// 相同工单名称可能有多条数据,这里只保存工单Id去查询Id最大的那个工单,也就是最近的那个工单.
        /// </summary>
        /// <param name="recipeName"></param>
        /// <returns></returns>
        public static DataResult<OrderEntity> GetOrderByRecipeName(string recipeName)
        {
            DataResult<OrderEntity> result = new DataResult<OrderEntity>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.Orders
                        .Where(order => order.RecipeName == recipeName)
                        .OrderByDescending(order => order.Id)
                        .FirstOrDefault();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 根据Id获取对应的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static DataResult<OrderEntity> GetOrderById(int id)
        {
            DataResult<OrderEntity> result = new DataResult<OrderEntity>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.Orders.SingleOrDefault(order => order.Id == id);
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        public static DataResult<List<OrderEntity>> GetOrders()
        {
            DataResult<List<OrderEntity>> result = new DataResult<List<OrderEntity>>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.Orders.OrderBy(order => order.RecipeName).ToList();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 添加一条数据到Orders表中
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public static DataResult AddOrder(OrderEntity order)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    context.Orders.Add(order);
                    int affectedRows = context.SaveChanges();
                    if (affectedRows > 0)
                    {
                        result.State = true;
                    }
                    else
                    {
                        result.Message = ConstantManager.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 新增一个Order,并返回新增的这个Order的Id.
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public static DataResult<int> AddOrderWithIdBack(OrderEntity order)
        {
            DataResult<int> result = new DataResult<int>();
            try
            {
                using (var context = new DataContext())
                {
                    context.Orders.Add(order);
                    int affectedRows = context.SaveChanges();
                    if (affectedRows > 0)
                    {
                        result.State = true;
                        result.Data = order.Id;
                    }
                    else
                    {
                        result.Message = ConstantManager.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        public static DataResult DeleteOrderById(int id)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    var order = context.Orders.Find(id);
                    if (order != null)
                    {
                        context.Orders.Remove(order);
                        int affectRows = context.SaveChanges();
                        if (affectRows > 0)
                        {
                            result.State = true;
                        }
                        else
                        {
                            result.Message = "数据删除失败";
                        }
                    }
                    else
                    {
                        result.Message = "请选中要删除的工单!";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 根据工单实体更新具体的工单信息
        /// </summary>
        /// <param name="newOrder"></param>
        /// <returns></returns>
        public static DataResult UpdateOrder(OrderEntity newOrder)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    // 查询要更新的数据是否存在
                    var existingOrder = context.Orders.FirstOrDefault(order => order.Id == newOrder.Id);
                    if (existingOrder != null)
                    {
                        // 更新实体的各个属性
                        context.Entry(existingOrder).CurrentValues.SetValues(newOrder);
                        int affectRows = context.SaveChanges();
                        if (affectRows > 0)
                        {
                            result.State = true;
                        }
                        else
                        {
                            result.Message = "数据库更新失败,要更新的数据和原数据相同";
                        }
                    }
                    else
                    {
                        result.Message = ConstantManager.DatabaseUpdateError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 根据工单输入的搜索Id,去查询响应的工单结果
        /// </summary>
        /// <param name="searchRecipeId"></param>
        /// <returns></returns>
        public static DataResult<List<OrderEntity>> GetOrdersByRecipeIdLikely(string searchRecipeId)
        {
            DataResult<List<OrderEntity>> result = new DataResult<List<OrderEntity>>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.Orders
                        .Where(order => order.RecipeName!.ToLower().Contains(searchRecipeId.ToLower()))
                        .OrderBy(order => order.RecipeName)
                        .ToList();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }
    }
}
